﻿/*
   Open Intel 
   Copyright © 2011 – ISC. All Rights Reserved.
*/

using System.Linq;
using System.Net;
using Microsoft.WindowsAzure.ServiceRuntime;

namespace OI.Web
{
    public class WebRole : RoleEntryPoint
    {
        public override bool OnStart()
        {
            // Set the maximum number of concurrent connections 
            ServicePointManager.DefaultConnectionLimit = 8;

            //DiagnosticMonitorConfiguration diagConfig =
               //DiagnosticMonitor.GetDefaultInitialConfiguration();

            //diagConfig.PerformanceCounters.DataSources.Add(
            //    new PerformanceCounterConfiguration()
            //    {
            //        CounterSpecifier = @"\Processor(_Total)\% Processor Time",
            //        SampleRate = TimeSpan.FromMinutes(10)
            //    });

            //diagConfig.PerformanceCounters.DataSources.Add(
            //    new PerformanceCounterConfiguration()
            //    {
            //        CounterSpecifier = @"\Memory\Available Mbytes",
            //        SampleRate = TimeSpan.FromMinutes(10)
            //    });

            //diagConfig.PerformanceCounters.ScheduledTransferPeriod =
            //    TimeSpan.FromMinutes(10);

            //diagConfig.Logs.ScheduledTransferLogLevelFilter = LogLevel.Undefined;
            //diagConfig.Logs.ScheduledTransferPeriod = TimeSpan.FromMinutes(1);
            
            //// uncomment for IIS logs
            ////diagConfig.Directories.ScheduledTransferPeriod = TimeSpan.FromMinutes(10);

            ////Add the Windows Event Log data source to the default initial configuration
            //diagConfig.WindowsEventLog.DataSources.Add("Application!*");

            ////Specify the scheduled transfer
            //diagConfig.WindowsEventLog.ScheduledTransferPeriod = TimeSpan.FromMinutes(10);

            //DiagnosticMonitor.Start("DiagnosticsConnectionString", diagConfig);
            //System.Diagnostics.Trace.TraceInformation("Done configuring diagnostics.");

            // For information on handling configuration changes 
            // see the MSDN topic at http://go.microsoft.com/fwlink/?LinkId=166357. 
            RoleEnvironment.Changing += RoleEnvironmentChanging;

            return base.OnStart();
        }


        public override void OnStop()
        {
            System.Diagnostics.Trace.TraceWarning("Onstop called.");
            base.OnStop();
        }


        private void RoleEnvironmentChanging(object sender, RoleEnvironmentChangingEventArgs e)
        {
            // If a configuration setting is changing
            if (e.Changes.Any(change => change is RoleEnvironmentConfigurationSettingChange))
            {
                // Set e.Cancel to true to restart this role instance
                e.Cancel = true;
            }
        }
    }
}